/**
 * @file: proc.cpp
 * @author: yumeng (imyumeng@qq.com)
 * @brief:
 * @version 0.1
 * @date: 2021-09-06
 *
 * @copyright Meng_Yu Copyright (c) 2021
 *
 * @history: init first version. 2021-09-06
 */
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "proc.h"

proc_data_t g_data;

int Read_Tnifile(void)
{
	char tmp_str[STR_LEN] = {0};
	g_data.bin_num = GetPrivateProfileInt(INI_SEC_BIN, INI_SEC_BIN_NUM, 0x1, g_data.ini_path_name);
	//printf("bin_num:%d\r\n", g_data.bin_num);
	if (g_data.bin_num > BIN_NUM)
	{
		printf("[E]bin_num max is:%d\r\n", BIN_NUM);
		return -1;
	}
	for (int i = 0; i < g_data.bin_num; i++)
	{
		sprintf(tmp_str, "bin%d", i);
		g_data.pre_len[i] = GetPrivateProfileInt(tmp_str, INI_SEC_SUB_BIN_PRELEN, 0x0, g_data.ini_path_name);
		g_data.align_len[i] = GetPrivateProfileInt(tmp_str, INI_SEC_SUB_BIN_LEN, 0x1000, g_data.ini_path_name);
		g_data.fill_pat[i] = GetPrivateProfileInt(tmp_str, INI_SEC_SUB_BIN_PAT, 0, g_data.ini_path_name);
		GetPrivateProfileString(tmp_str, INI_SEC_SUB_BIN_IN, INI_DEF_STR, (LPTSTR)g_data.bin_in[i], 80, g_data.ini_path_name);
		GetPrivateProfileString(tmp_str, INI_SEC_SUB_BIN_OUT, INI_DEF_STR, (LPTSTR)g_data.bin_out[i], 80, g_data.ini_path_name);
	}
	return 0;
}

int Write_Tnifile(void)
{
	char tmp_str[STR_LEN] = {0};
	char tmp_str1[STR_LEN] = {0};
	sprintf(tmp_str, "%d", g_data.bin_num);
	WritePrivateProfileString(INI_SEC_BIN, INI_SEC_BIN_NUM, tmp_str, g_data.ini_path_name);
	for (int i = 0; i < g_data.bin_num; i++)
	{
		sprintf(tmp_str, "bin%d", i);
		sprintf(tmp_str1, "0x%x", g_data.align_len[i]);
		WritePrivateProfileString(tmp_str, INI_SEC_SUB_BIN_LEN, tmp_str1, g_data.ini_path_name);
		sprintf(tmp_str1, "0x%x", g_data.pre_len[i]);
		WritePrivateProfileString(tmp_str, INI_SEC_SUB_BIN_PRELEN, tmp_str1, g_data.ini_path_name);
		sprintf(tmp_str1, "0x%x", g_data.fill_pat[i]);
		WritePrivateProfileString(tmp_str, INI_SEC_SUB_BIN_PAT, tmp_str1, g_data.ini_path_name);
		WritePrivateProfileString(tmp_str, INI_SEC_SUB_BIN_IN, (LPTSTR)g_data.bin_in[i], g_data.ini_path_name);
		WritePrivateProfileString(tmp_str, INI_SEC_SUB_BIN_OUT, (LPTSTR)g_data.bin_out[i], g_data.ini_path_name);
	}
	return 0;
}

void path_proc(char *path)
{
	char *p = path;
	while (*p != 0)
	{
		if('\\' == *p)
		{
			*p = '/';
		}
		p++;
	}
}
